const appUrl=require("../utils/http")
function formatTime(timestamp) {
	var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '' : date.getMonth() + 1) + '-';
	if (date.getDate() + '' < 10) {
		var D = '0' + date.getDate() + ' ';
	} else {
		var D = date.getDate() + ' ';
	}
	return Y + M + D;
}

function verifyPhoneNumber(jump = true,showToast=true) {
	const userInfo = uni.getStorageSync('userInfo')
	let callback = null
	if (!userInfo) {
		if(showToast){
			uni.showToast({
				title: '您还未登录，请先登录',
				icon: 'none'
			});
		}
		
		if (jump) {
			callback = setTimeout(() => {
				// uni.reLaunch({
				// 	url: '/pages/login/login'
				// });
				const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				if (currentPage.route != 'pages/login/login') {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}
			}, 1000);
		} else {
			callback = true
		}
		return callback;
	}
	if (!userInfo?.mobile) {
		if(showToast){
			uni.showToast({
				title: '未检测到手机号，请重新登录',
				icon: 'none'
			});
		}
		
		if (jump) {
			callback = setTimeout(() => {
				const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				if (currentPage.route != 'pages/login/login') {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}
				// uni.reLaunch({
				// 	url: '/pages/login/login'
				// });
			}, 1000);
		} else {
			callback = true
		}
		return callback;
	}
	return callback
}

function feedbackError({custom_data,message}) {
	let userInfo = uni.getStorageSync('userInfo')
	let data = {
		app_version: "",
		platform: "",
		os_version: "",
		device_model: "",
		error_type: "JS_ERROR",
		error_message: message,
		stack_trace: message,
		page_url: "",
		uid: "",
		network_type: "",
		api_url: "",
		api_params: "",
		custom_data
	}
	const pages = getCurrentPages();
	const currentPage = pages[pages.length - 1]
	data.page_url = currentPage.route
	data.uid = userInfo.id
	uni.getSystemInfo({
		success: function(res) {
			data.app_version = res.appVersion
			data.platform = res.osName
			data.os_version = res.osVersion
			data.device_model = res.deviceModel
			// data.custom_data = {
			// 	romName: res.romName,
			// 	romVersion: res.romVersion
			// }
		}
	});
	uni.getNetworkType({
		success: function(res) {
			data.network_type = res.networkType
		}
	});
	// http.request('user/log', data).then((res) => {}).catch((err) => {
	// 	uni.showToast({
	// 		title: err?.message,
	// 		icon: "none"
	// 	})
	// });
	let s = uni.getStorageSync('token') || '';
	let h = {
		'content-type': 'application/json'
	};
	if (s) {
		h.token = s;
	}
	console.log(appUrl.appUrl)
	uni.request({
		url: appUrl.appUrl + '/user/log',
		//服务器url+参数中携带的接口具体地址
		data,
		//请求参数
		method: 'POST',
		header: h
	});
}
module.exports = {
	formatTime,
	verifyPhoneNumber,
	feedbackError
};